package com.leetcode.array;

public class LongestAlternatingSubarray {

    public static void main(String[] args) {
        LongestAlternatingSubarray longest = new LongestAlternatingSubarray();
        int[] nums = {2, 3, 3, 10};
        int threshold = 18;
        System.out.println(longest.longestAlternatingSubarray(nums, threshold));
    }
    public int longestAlternatingSubarray(int[] nums, int threshold) {
        int res = 0;
        int left = 0;
        int right = 0;
        boolean flag = false;
        for (int i = 0; i < nums.length; i++) {
            if (!flag) {
                if (nums[i] > threshold || nums[i] % 2 != 0) {
                    res = Math.max(res, right - left);
                    left = right;
                    continue;
                }
                right++;
                flag = true;
                continue;
            }
            if (flag) {
                flag = false;
                if (nums[i] > threshold || nums[i] % 2 == 0) {
                    res = Math.max(res, right - left);
                    left = right;
                    i--;
                    continue;
                }
                right++;
            }
        }
        res = Math.max(res, right - left);
        return  res;
    }
}
